//
//  Shop_ViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/1.
//  Copyright © 2016年 王道林. All rights reserved.
//
#import "DLMacro.h"
#import "Shop_ViewController.h"
#import "BaseCollectionView.h"
#import "Shop_collectionViewCell.h"
#import "DLWNetworkingParser.h"
#import "Home_NewsViewControllerWeb.h"

@interface Shop_ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrData;
@end

@implementation Shop_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"积蛋商城";
    [self handleData];
    [self createSubView];
}
#pragma mark - 数据请求
- (void)handleData
{
    _arrData = [NSMutableArray array];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *ticket = [userDefault valueForKey:@"ticket"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:ticket,@"ticket", nil];
    [DLWNetworkingParser POSTWithURL:@"http://api.danjiguanjia.com/active/Index/goodlist" par:dic success:^(id responseObject) {
        
        for (NSDictionary *dic in responseObject[@"data"][@"data"]) {
            ShopGoodModel *model = [ShopGoodModel modelWithDic:dic];
            [_arrData addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}

#pragma mark - 创建子控件
- (void)createSubView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((VIEW_WIDTH - 15) / 2, 200);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(20, 5, 0, 5);
    _collectionView = [[BaseCollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor colorWithWhite:0.827 alpha:1.000];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[Shop_collectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}

#pragma mark -collctionView 的协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrData.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Shop_collectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.shopModel = _arrData[indexPath.row];
    return cell;
}


// 下面的商品点击.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
        ShopGoodModel *model = _arrData[indexPath.row];
        Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
        webVC.name = @"shopGoodsDetail";
        webVC.newsId = model.goodID;
        webVC.navigationTitle = @"商品详情";
        [self.navigationController pushViewController:webVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
